package cn.iocoder.yudao.module.shop.staff.api;

import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.shop.admin.ShopUserApi;
import cn.iocoder.yudao.module.api.shop.admin.dto.ShopAdminRespDTO;
import cn.iocoder.yudao.module.api.shop.staff.ShopStaffApi;
import cn.iocoder.yudao.module.api.shop.staff.dto.ShopStaffSaveDTO;
import cn.iocoder.yudao.module.api.shop.staff.vo.ShopStaffVO;
import cn.iocoder.yudao.module.shop.staff.pojo.po.ShopStaff;
import cn.iocoder.yudao.module.shop.staff.service.ShopStaffService;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  这里是文件说明
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/03/31
 */
@Service
@Validated
public class ShopStaffApiImpl implements ShopStaffApi {
    @Resource
    private ShopStaffService shopStaffService;

    @Resource
    @Lazy // 延迟加载，避免循环依赖
    private ShopUserApi shopUserApi;

    @Override
    public ShopStaffVO getById(Long id) {
        if (id == null || id <= 0) {
            return null;
        }
        ShopStaff verifyStaffDO = shopStaffService.getStaff(id);
        return BeanUtils.toBean(verifyStaffDO, ShopStaffVO.class);
    }

    @Override
    public ShopStaffVO getByUserId(Long shopId, Long userId, Integer userType) {
        ShopStaffVO respDTO = new ShopStaffVO();
        respDTO.setShopId(shopId);
        if (UserTypeEnum.MEMBER.getValue().equals(userType)) {
            respDTO.setMemberUserId(userId);
        } else {
            respDTO.setAdminUserId(userId);
        }
        // 检查店铺商户身份
        ShopAdminRespDTO adminRespDTO = shopUserApi.getAdmin(shopId, userId, userType);
        if (adminRespDTO != null) {
            respDTO.setContactName(adminRespDTO.getContactName());
            respDTO.setContactMobile(adminRespDTO.getContactMobile());
            return respDTO;
        }
        // 检查核销员身份
        ShopStaff verifyStaffDO;
        if (UserTypeEnum.MEMBER.getValue().equals(userType)) {
            verifyStaffDO = shopStaffService.getByMemberUserId(shopId, userId);
        } else {
            verifyStaffDO = shopStaffService.getByAdminUserId(shopId, userId);
        }
        return BeanUtils.toBean(verifyStaffDO, ShopStaffVO.class);
    }

    @Override
    public ShopStaffVO getByMobile(Long shopId, String mobile) {
        ShopStaff verifyStaffDO = shopStaffService.getByMobile(shopId, mobile);
        return BeanUtils.toBean(verifyStaffDO, ShopStaffVO.class);
    }

    @Override
    public ShopStaffVO getByUserId(Long userId, Integer userType, @Nullable Integer role) {
        ShopStaff verifyStaffDO = shopStaffService.getByUserId(userId, userType, role);
        return BeanUtils.toBean(verifyStaffDO, ShopStaffVO.class);
    }

    @Override
    public List<ShopStaffVO> getByMemberUserId(Long userId) {
        List<ShopStaff> staffs = shopStaffService.getByMemberUserId(userId);
        return BeanUtils.toBean(staffs, ShopStaffVO.class);
    }

    @Override
    public boolean isStaff(Long userId, Integer userType, @Nullable Integer role) {
        if (role != null) {
            return shopStaffService.isStaff(userId, userType, role);
        }
        return shopStaffService.isStaff(userId, userType, null) || shopUserApi.isShopAdmin(userId, userType);
    }

    @Override
    public void saveStaff(ShopStaffSaveDTO dto) {
        shopStaffService.saveStaff(dto);
    }

    @Override
    public List<ShopStaffVO> getByShopIdAndRole(Long shopId, @Nullable Integer role) {
        List<ShopStaff> staffs = shopStaffService.getByShopId(shopId, role);
        return BeanUtils.toBean(staffs, ShopStaffVO.class);
    }

    @Override
    public void updateStaffStatus(Long id, Integer status) {
        shopStaffService.updateStaffStatus(id, status);
    }

}
